﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using AMM.Business;
using AMM.Business.Config;
using AMM.CustomClassLibrary;
using AMM.CustomWinComponents.Validator;
using AMM.CustomWinControls;
using AMM.Model;
using AMM.Model.Config;

namespace AMM.Forms.Config {
    public partial class FrmBasics : FrmBasicsBase {

        private BasicsType _basicsType;
        private BasicsProcessor _basicsProcessor;
        private List<BasicsViewModel> _basicsList;

        #region Override

        protected override string FunctionCode {
            get { return "AMM1103"; }
        }

        protected override void Button_Click(object sender, EventArgs e) {
            Button btn = sender as Button;
            switch (btn.Name) {
                case "btnLoad":
                    this._basicsType = (BasicsType)this.cmbBasicsType.SelectedValue;
                    this._basicsList = this._basicsProcessor.FetchBasicsByType(this._basicsType);
                    this.BindBasicsList();
                    break;
            }
        }

        protected override void Add() {
            this.ResetForms();
            this.FBS.CurrentItem = new Basic() {
                TypeEnum = (int)this._basicsType,
                Sequence = this._basicsList.Count,
                IsEnabled = true,
                ClassStatus = ClassStatus.Added
            };

            this.gbBasicsInfo.Enabled = true;
            this.validator.ClearAllControlErrorIcons();
            base.Add();
        }

        protected override void Amend() {
            List<BasicsViewModel> selectedList = this.dgvBasicsList.GetSelectedRows<BasicsViewModel>();
            if (selectedList.Count > 1) { this.OnlySelectSingleRecord(); return; }
            if (selectedList.Count == 0) { this.ShouldSelectSingleRecord(); return; }
            Basic basics = this._basicsProcessor.FetchBasicsById(selectedList[0].Key);
            basics.ClassStatus = ClassStatus.Updated;
            this.FBS.CurrentItem = basics;
            this.gbBasicsInfo.Enabled = true;
            base.Amend();
        }

        protected override void Save() {
            base.Save();
            this.CreateNewThread(() => {
                this.PreventCancel = true;
                Basic basics = this.FBS.CurrentItem;

                if (basics.ClassStatus == ClassStatus.Added) {
                    this._basicsProcessor.Insert(basics);
                }
                else {
                    this._basicsProcessor.Update(basics);
                }

                this._basicsList = this._basicsProcessor.FetchBasicsByType(this._basicsType);
                this.MDIParent.SetStatusInformation("保存成功");
            }, () => {
                this.ResetForms();
                this.PreventCancel = false;
            });
        }

        #endregion

        public FrmBasics() {
            this.InitializeComponent();

            this.dgvBasicsList.DataGridViewModelType = typeof(BasicsViewModel);
        }

        #region Private methods

        private void ResetForms() {
            this.BindBasicsList();
            this.FBS.CurrentItem = null;
            this.gbBasicsInfo.Enabled = false;
            this.cmbBasicsType.SelectedValue = (int)this._basicsType;
        }

        private void CreateFormBindings() {
            this.FBS.CreateBinding(this.tbCode, x => x.Text, y => y.Code);
            this.FBS.CreateBinding(this.tbName, x => x.Text, y => y.Name);
            this.FBS.CreateBinding(this.tbDescription, x => x.Text, y => y.Description);
            this.FBS.CreateBinding(this.cbIsEnabled, x => x.Checked, y => y.IsEnabled);
            this.FBS.CreateBinding(this.tbSequence, x => x.Text, y => y.Sequence);
            this.FBS.CreateBinding(this.cmbBasicsType, x => x.SelectedValue, y => y.TypeEnum);
        }

        private void BindBasicsList() {
            this.dgvBasicsList.DataSource = new SortableBindingList<BasicsViewModel>(this._basicsList);
        }

        private void BindValidation() {
            this.validator.SetType(this.tbCode, ValidationType.Required | ValidationType.RegularExpression);
            this.validator.SetRequiredMessage(this.tbCode, this.RequiredMessage);
            this.validator.SetRegularExpression(this.tbCode, "^[A-Za-z0-9-]+$");
            this.validator.SetRegularExpressionMessage(this.tbCode, "编码只能由字母，数字，连接线组成");

            this.validator.SetType(this.tbName, ValidationType.Required);
            this.validator.SetRequiredMessage(this.tbName, this.RequiredMessage);

            this.validator.SetType(this.tbSequence, ValidationType.Required);
            this.validator.SetRequiredMessage(this.tbSequence, this.RequiredMessage);
        }

        private void BindBasicsType() {
            this.cmbBasicsType.DataSource = this._basicsProcessor.FetchBasicsType();
            this.cmbBasicsType.DisplayMember = "Text";
            this.cmbBasicsType.ValueMember = "Value";
        }

        #endregion

        private void FrmBasics_Load(object sender, EventArgs e) {
            this.CreateNewThread(() => {
                this.PreventCancel = true;
                this.FBS = new FlexBindingSource<Basic>();

                this._basicsType = BasicsType.Currency;
                this._basicsProcessor = new BasicsProcessor();
                this._basicsList = this._basicsProcessor.FetchBasicsByType(this._basicsType);
            }, () => {
                this.WindowState = FormWindowState.Maximized;

                this.BindBasicsType();
                this.BindBasicsList();
                this.CreateFormBindings();
                this.BindValidation();
                this.ResetForms();
                this.PreventCancel = false;
            });
        }
    }
}
